<template>
  <el-dialog
    class="add-mulcontractors-comp"
    :title="title"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(800)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        element-loading-custom-class="custom-load"
        :model="ruleForm"
        :rules="rules"
        size="medium"
        ref="ruleForm"
        :label-width="$setRem(120)"
        class="rule-form"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目参建方" prop="attrId" required>
              <el-select
                filterable
                v-model="ruleForm.attrId"
                placeholder="请选择项目参建方">
                <el-option
                  v-for="item in contractors"
                  :key="item.id"
                  :label="item.projectAttr"
                  :value="item.id">
                  {{ item.projectAttr }}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构名称" prop="orgId" required>
              <el-select
                filterable
                v-model="ruleForm.orgId"
                placeholder="请选择机构名称">
                <el-option
                  v-for="item in orgList"
                  :key="item.id"
                  :label="item.orgName"
                  :value="item.id">
                  {{ item.orgName }}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="参建方排序" prop="sort">
              <el-input
                type="text"
                placeholder="参建方排序"
                v-model="ruleForm.sort"
                clearable>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="4"
                placeholder="请输入备注"
                v-model="ruleForm.remark"
                clearable>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleCancel">取 消</el-button>
      <el-button
        size="medium"
        type="primary"
        :loading="loading"
        @click="hanldeSave">
        保 存
      </el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: false
    },
    option: {
      type: Object,
      default: () => ({})
    },
    loading: {
      type: Boolean,
      default: false
    },
    projectId: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'add'
    },
  },
  data() {
    return {
      contractors: [], // 项目参建方
      orgList: [], // 机构
      ruleForm: {
        id: '',
        attrId: '',
        orgId: '',
        remark: '',
        sort: ''
      },
      rules: {
        attrId: [
          { required: true, message: '请选择项目参建方' },
        ],
        // orgId: [
        //   { required: true, message: '请选择机构名称', },
        // ],
      },
    };
  },
  computed: {
    // 设置弹框标题
    title() {
      return `${this.type === 'add' ? '新增' : '编辑'}`;
    },
  },
  watch: {
    // 监听弹框显示/隐藏
    visible(value) {
      if (!value) {
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] = '';
        });
        return;
      }
      this.getContractors();
      this.getOrgPageList();
    },
    // 回填数据
    option: {
      deep: true,
      handler: function (data) {
        const keys = Object.keys(this.ruleForm);
        data && Object.keys(data).length && keys.forEach(key => {
          this.ruleForm[key] = data[key] || '';
        });
      }
    }
  },
  methods: {
    // 获取参建方数据
    async getContractors() {
      const { success, data: {
        data
      }} = await this.$Model.getContractors({
        projectId: this.projectId,
        pageFlag: false
      });
      if (success && data) {
        this.contractors = data.list || [];
      }
    },
    async getOrgPageList() {
      const { success, data: {
        data
      }} = await this.$Model.getOrgPageList({
        projectId: this.projectId,
        pageFlag: false
      });
      if (success && data) {
        this.orgList = data.list || [];
      }
    },
    // 保存
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', {
            projectId: this.projectId,
            ...this.ruleForm
          });
        }
      });
    },
    // 取消
    handleCancel() {
      this.$emit('cancel');
    }
  }
};
</script>

<style lang="scss" scoped>
  .add-mulcontractors-comp {
    .form-container {
      width: 100%;
      display: flex;
      align-items: center;
      padding-right: 20px;
      .el-form {
        width: 100%;
        .el-row {
          margin-bottom: 10px;
        }
      }
    }
  }
</style>
